package com.hlt.shuiliting.servlet;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hlt.shuiliting.dao.GongChengXMDAO;
import com.hlt.shuiliting.dao.ZhongDaSJBGDAO;
import com.hlt.shuiliting.valuebean.ZhongDaSJBG;

public class ZhongDaSJBGServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public ZhongDaSJBGServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");//�������Ĳ���
		response.setCharacterEncoding("UTF-8");//�������Ĳ���
		response.setContentType("text/html");
		
		String servletPath = request.getServletPath();
		if (servletPath.equalsIgnoreCase("/listZhongDaSJBG")) {
			 listZhongDaSJBG(request, response);
		} else if (servletPath.equalsIgnoreCase("/viewZhongDaSJBG")) {
			viewZhongDaSJBG(request, response);
		} else if (servletPath.equalsIgnoreCase("/insertZhongDaSJBG")) {
			insertZhongDaSJBG(request, response);
		} else if (servletPath.equalsIgnoreCase("/updateZhongDaSJBG")) {
			updateZhongDaSJBG(request, response);
		} else if (servletPath.equalsIgnoreCase("/deleteZhongDaSJBG")) {
			deleteZhongDaSJBG(request, response);
		}
		
	}
	
	public void listZhongDaSJBG(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ZhongDaSJBGDAO zhongDaSJBGDAO = new ZhongDaSJBGDAO();
		GongChengXMDAO gongChengXMDAO = new GongChengXMDAO();
		String pageSize = request.getParameter("pageSize");
		String pageNum = request.getParameter("pageNum");
		String GongChengXMID = request.getParameter("GongChengXMID");
		
		List list = zhongDaSJBGDAO.list(Integer.parseInt(GongChengXMID), (Integer.parseInt(pageSize)), (Integer.parseInt(pageNum)));
		Map map = (Map)gongChengXMDAO.view(Integer.parseInt(GongChengXMID));
		String pageCount = Integer.toString(zhongDaSJBGDAO.getPageCount());
		
		request.setAttribute("pageCount", pageCount);
		request.setAttribute("pageNum", pageNum);
		request.setAttribute("gongChengXM", map);
		request.setAttribute("zhongDaSJBGLIST", list);
		
		request.getRequestDispatcher("/ZhongDaSJBG_List.jsp").forward(request, response);
	}
	
	public void viewZhongDaSJBG(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ZhongDaSJBGDAO zhongDaSJBGDAO = new ZhongDaSJBGDAO();
		String ID = request.getParameter("ID");
		String viewOrUpdate = request.getParameter("viewOrUpdate");
		Map map = zhongDaSJBGDAO.view(Integer.parseInt(ID));
		request.setAttribute("zhongDaSJBG", map);
		
		if (viewOrUpdate.equalsIgnoreCase("view")) {
			request.getRequestDispatcher("ZhongDaSJBG_View.jsp").forward(request, response);
		} else if (viewOrUpdate.equalsIgnoreCase("update")) {
			request.getRequestDispatcher("ZhongDaSJBG_Update.jsp").forward(request, response);
		}
	}
	
	public void insertZhongDaSJBG(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ZhongDaSJBGDAO zhongDaSJBGDAO = new ZhongDaSJBGDAO();
		ZhongDaSJBG zhongDaSJBG = new ZhongDaSJBG();
		
		String ZhuangTai = request.getParameter("ZhuangTai");
		String JiLuSJ = request.getParameter("JiLuSJ");
		String LuRuR = request.getParameter("LuRuR");
		String SheJiXMBGSPSJ = request.getParameter("SheJiXMBGSPSJ");
		String BianGengYY = request.getParameter("BianGengYY");
		String TouZhiBH = request.getParameter("TouZhiBH");
		String BianGengNR = request.getParameter("BianGengNR");
		String ShenPiDW = request.getParameter("ShenPiDW");
		String ShenPiWH = request.getParameter("ShenPiWH");
		String GongChengXMID = request.getParameter("GongChengXMID");
		
		zhongDaSJBG.setZhuangTai(ZhuangTai);	
		zhongDaSJBG.setJiLuSJ(JiLuSJ);	
		zhongDaSJBG.setLuRuR(LuRuR);	
		zhongDaSJBG.setSheJiXMBGSPSJ(SheJiXMBGSPSJ);	
		zhongDaSJBG.setBianGengYY(BianGengYY);	
		zhongDaSJBG.setTouZhiBH(TouZhiBH);	
		zhongDaSJBG.setBianGengNR(BianGengNR);	
		zhongDaSJBG.setShenPiDW(ShenPiDW);	
		zhongDaSJBG.setShenPiWH(ShenPiWH);	
		zhongDaSJBG.setGongChengXMID(GongChengXMID);
		
		zhongDaSJBGDAO.insert(zhongDaSJBG);
		
		response.sendRedirect("listZhongDaSJBG?GongChengXMID=" + GongChengXMID);
	}

	public void updateZhongDaSJBG(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ZhongDaSJBGDAO zhongDaSJBGDAO = new ZhongDaSJBGDAO();
		ZhongDaSJBG zhongDaSJBG = new ZhongDaSJBG();
		
		String ID = request.getParameter("ID");
		String ZhuangTai = request.getParameter("ZhuangTai");
		String JiLuSJ = request.getParameter("JiLuSJ");
		String LuRuR = request.getParameter("LuRuR");
		String SheJiXMBGSPSJ = request.getParameter("SheJiXMBGSPSJ");
		String BianGengYY = request.getParameter("BianGengYY");
		String TouZhiBH = request.getParameter("TouZhiBH");
		String BianGengNR = request.getParameter("BianGengNR");
		String ShenPiDW = request.getParameter("ShenPiDW");
		String ShenPiWH = request.getParameter("ShenPiWH");
		String GongChengXMID = request.getParameter("GongChengXMID");
		
		zhongDaSJBG.setID(ID);
		zhongDaSJBG.setZhuangTai(ZhuangTai);	
		zhongDaSJBG.setJiLuSJ(JiLuSJ);	
		zhongDaSJBG.setLuRuR(LuRuR);	
		zhongDaSJBG.setSheJiXMBGSPSJ(SheJiXMBGSPSJ);	
		zhongDaSJBG.setBianGengYY(BianGengYY);	
		zhongDaSJBG.setTouZhiBH(TouZhiBH);	
		zhongDaSJBG.setBianGengNR(BianGengNR);	
		zhongDaSJBG.setShenPiDW(ShenPiDW);	
		zhongDaSJBG.setShenPiWH(ShenPiWH);	
		zhongDaSJBG.setGongChengXMID(GongChengXMID);
		
		zhongDaSJBGDAO.update(zhongDaSJBG);
		
		response.sendRedirect("listZhongDaSJBG?GongChengXMID=" + GongChengXMID);
	}
	
	public void deleteZhongDaSJBG(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ZhongDaSJBGDAO zhongDaSJBGDAO = new ZhongDaSJBGDAO();
		String ID = request.getParameter("ID");
		String GongChengXMID = request.getParameter("GongChengXMID");
		zhongDaSJBGDAO.delete(Integer.parseInt(ID));
		response.sendRedirect("listZhongDaSJBG?GongChengXMID=" + GongChengXMID);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
